<template>
  <div class="addform">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-position="right"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="重点:">
        <el-switch v-model="disabled"></el-switch>
      </el-form-item>
      <el-form-item label="秘密标题:" prop="secretTitle">
        <el-input v-model="ruleForm.secretTitle" class="inputS"></el-input>
      </el-form-item>
      <el-form-item label="秘密介绍:" prop="secretCon">
        <el-input
          type="textarea"
          v-model="ruleForm.secretCon"
          :rows="4"
          class="inputL"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >新增秘密</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      disabled: false,
      successVisible: false,
      ruleForm: {//保证前后端名称相同
        id: 1,
        secretTitle: "",
        secretCon: "",
        secretNumber: 1
      },
      rules: {//如果没输入的提示信息
        secretTitle: [
          { required: true, message: "请输入秘密标题", trigger: "blur" },
          { max: 50, message: "最大长度50个字", trigger: "blur" },
        ],
        secretCon: [
          { required: true, message: "请输入内容", trigger: "blur" }
        ]
      },
    };
  },
  methods: {
    submitForm(formName) {
      if (this.disabled) {
        this.ruleForm.secretNumber = 1;
      } else {
        this.ruleForm.secretNumber = 0;
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 由于axios内部必须使用_this才能使用this
          const _this = this;
          this.axios({
            method: "post",
            url: _this.baseUrl + "/index2/post/addSecret",
            //传递给后端的数据
            data: _this.ruleForm
          }).then(function (resp) {
            console.log(resp.data);
            _this.$parent.$parent.successAdd();//提交成功提示
          });
        } else {
          console.log("修改失败");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
};
</script>

<style lang="less" scoped>
.addform {
  width: 100%;
  margin: 0 auto;
  .inputS {
    width: 80%;
  }
  .inputL {
    width: 80%;
    display: block;
  }
  .imgPreview {
    width: 150px;
    height: 150px;
    margin-right: 15px;
    float: left;
    cursor: pointer;
  }
}
</style>